import { _decorator, Component } from 'cc';
import { State, WSConnectedEvent, WSRecvDataEvent } from '../../../../Framework/Scripts/Managers/Net/NetCfg';
import { CocosUtils } from '../../../../Framework/Scripts/Utils/CocosUtils';
import { UIMgr, UIType } from '../../../../Framework/Scripts/Managers/UIMgr';
import { AbNames, EventMsgKes } from '../../Config/ResConfig';
import { GameData } from '../../Data/GameData';
import {IAnswerTzFuHuoReq, IAnswerTzReq, IAnswerTzRes, IAnswerTzTimeOutReq, IAnswerTzTimeOutRes, IAsTzEndGetRewardReq, IAsTzEndGetRewardRes, IFuHuoRes, IGmaeSceneZtRes, INoticeGameEnd, INoticeNext } from '../proto/gameTz';
const { ccclass, property } = _decorator;

var CmdTz = {
    GameSceneTzMsg: 9044, // 每日挑战场景
    StartAnswerTzMsg:9045, ///挑战赛开始答题
    StartAnswerTzErrNotice:9047, //答错通知正确的结果
    StartAnswerTzTimeOutReq:9048, //每日挑战答题超时
    StartAnswerTzNextNotice:9049, //每日挑战答题通知下一题
    StartAnswerTzEndNotice:9050,//结算通知

    StartAnswerTzEndGetRewardReq:9052, //结算有奖励领取奖励
    StartAnswerTzFuHuoReq:9053, //复活
}


//每日挑战-----------消息
@ccclass('GameTzMsg')
export class GameTzMsg extends Component {
    net: any;
    url: string;
    start() {
        // this.InitUI();
        // this.InitData();

    }

    update(deltaTime: number) {
        if(this.net.state === State.Invalid) {
            return;
        }
        if(this.net.state === State.Disconnected) {
            console.log('=====游戏服务《每日挑战》重新连接==========')
            this.net.connectServer( this.url);
        }
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    public ConnectGameTz(ip,port){
        this.InitUI();
        this.InitData();

        this.url = CocosUtils.getUrlByIdAndPort(ip,port);
        console.log('======开始链接游戏（每日挑战）服务=======',this.url);
        this.net = new globalThis.gameMgr.WsMgr();
        this.net.connectServer(this.url);
    }

    private InitUI(){
        this.registerEvent();       
    }

    private InitData(){
        globalThis.gameMgr._GameTzMsg = this;
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(WSConnectedEvent,this.onConnectGameServer,this);
        globalThis.gameMgr._eventTarget.on(WSRecvDataEvent,this.onRevGameData,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(WSConnectedEvent,this.onConnectGameServer,this);
        globalThis.gameMgr._eventTarget.on(WSRecvDataEvent,this.onRevGameData,this);
    }

    //发送JSON数据
    private _sendJson(type: number, data: any){
        console.log('=======_sendJson======',type,data);
        this.net.sendJsonData(type,data);
    }

    //大厅服务网络连接成功
    private onConnectGameServer(data){
        console.log('=======onConnectGameServer Tz===========');


        //请求场景消息
        this.sendGetScene();
    }


    //请求场景
    public sendGetScene(){
        let req = {
            userId: GameData.inst.userInfo.userId,
        }
        this._sendJson(CmdTz.GameSceneTzMsg,req);
    }

    //每日挑战请求答题
    public StartAnswerTzReq(tid: number, cid: number){
        let req: IAnswerTzReq = {
            userId: GameData.inst.userInfo.userId,
            tid: tid,
            cid: cid
        }
        console.log('=====StartAnswerTzReq========',req);
        this._sendJson(CmdTz.StartAnswerTzMsg, req);
    }

    //每日挑战请求答题超时
    public StartAnswerTzTimeOutReq(tid: number){
        let req: IAnswerTzTimeOutReq = {
            userId: GameData.inst.userInfo.userId,
            tid: tid,
        }
        this._sendJson(CmdTz.StartAnswerTzTimeOutReq,req);
    }

    //每日挑战--结算请求领取奖励
    public StartAnswerTzEndGetRewardReq(bei:number){
        let req:IAsTzEndGetRewardReq = {
            userId: GameData.inst.userInfo.userId,
            beiShu: bei
        }
        this._sendJson(CmdTz.StartAnswerTzEndGetRewardReq,req);
    }

    //每日挑战--复活 //
    public StartAnswerTzFuHuoReq(){
        let req: IAnswerTzFuHuoReq = {
            userId: GameData.inst.userInfo.userId,
            tid: 0
        }
        console.log('============StartAnswerTzFuHuoReq=======',req);
        this._sendJson(CmdTz.StartAnswerTzFuHuoReq,req);
    }

    
    //收到大厅服务数据
    private onRevGameData(binaryData){
        console.log('=======onRevGameData zt===========',binaryData);

        let data = JSON.parse(binaryData);
       
        let ctype = data.type;  //命令号
        let msg = data.data;

        this._onMsg(ctype,msg);
    }

    private _onMsg(ctype: number, data: any) {
        console.log('=====GameTzMsg==_onMsg========',ctype);
        switch (ctype) {
            case CmdTz.GameSceneTzMsg: //游戏场景
                this.onGameScene(data);
                break;
            case CmdTz.StartAnswerTzMsg: //答题返回
                this.onAnswerRes(data);
                break;    
            case CmdTz.StartAnswerTzNextNotice://下一题
                this.onNextTop(data);
                break;
            case CmdTz.StartAnswerTzErrNotice://答错 正确结果
                this.onCurRes(data);    
                break;
            case CmdTz.StartAnswerTzEndNotice: //通知结算
                this.onGameEnd(data);
                break;
            case CmdTz.StartAnswerTzEndGetRewardReq: //领取结算奖励
                this.onEndReward(data); 
                break;    
            case CmdTz.StartAnswerTzFuHuoReq: //复活
                this.onUserFuhuo(data);
                break;    
            case CmdTz.StartAnswerTzTimeOutReq: //答题超时
                this.onUsereAnswerTimeOut(data); 
                break; 
            default:
                break;
        }
    }

    // 游戏场景
    private onGameScene(data:IGmaeSceneZtRes){
        console.warn('=======onGameScene=======',data);
     
        //创景界面
        UIMgr.inst.ShowUIView(AbNames.GamePlay,'Prefabs/GameTZ/GamePlayTz',UIType.Full);
        let parent = UIMgr.inst.getParent(UIType.UnFull);
        parent.removeAllChildren();

        //发送消息
        globalThis.gameMgr._eventTarget.emit(EventMsgKes.msgGameSceneTzRes, data);
    }

    //答题返回
    private onAnswerRes(data: IAnswerTzRes){
        console.warn('=======onAnswerRes=======',data);
        globalThis.gameMgr._eventTarget.emit(EventMsgKes.msgStartAnswerTzRes, data);
    }

    // 下一题
    private onNextTop(data:INoticeNext){
        console.warn('=======onNextTop=======',data);
        globalThis.gameMgr._eventTarget.emit(EventMsgKes.msgStartAnswerTzNextNotice, data);
    }

    //答错 通知正确结果
    private onCurRes(data){
        console.warn('=======onCurRes=======',data);
        globalThis.gameMgr._eventTarget.emit(EventMsgKes.msgStartAnswerTzErrNotice, data);
    }

    //结算
    private onGameEnd(data:INoticeGameEnd){
        console.warn('=======onCurRes=======',data);
        globalThis.gameMgr._eventTarget.emit(EventMsgKes.msgStartAnswerTzEndNotice, data);
    }

    // 领取结算奖励
    private onEndReward(data:IAsTzEndGetRewardRes){
        console.warn('=======onEndReward=======',data);
        globalThis.gameMgr._eventTarget.emit(EventMsgKes.msgStartAnswerTzEndGetRewardRes, data);
    }

    //复活
    private onUserFuhuo(data:IFuHuoRes){
        console.warn('=======onUserFuhuo=======',data);
        globalThis.gameMgr._eventTarget.emit(EventMsgKes.msgStartAnswerTzFuHuoRes, data);
    }

    //用户答题超时返回
    private onUsereAnswerTimeOut(data:IAnswerTzTimeOutRes){
        console.warn('=======onUsereAnswerTimeOut=======',data);
        globalThis.gameMgr._eventTarget.emit(EventMsgKes.msgStartAnswerTzTimeOutRes, data);
    } 
}


